package tongcheng.com.tongcheng.base.fuwu;

import android.os.Bundle;
import android.support.v7.widget.AppCompatImageButton;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.adapter.FuwuAreaAdapter;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.FuWuAreaBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;

public class FuWuAreaActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.back_text)
    TextView mBackText;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.fuwuarea_ok)
    AppCompatImageButton mFuwuareaOk;
    @Bind(R.id.fuwuarea_rc)
    RecyclerView mFuwuareaRc;
    public List<FuWuAreaBean.DataBean> data = new ArrayList<>();
    private FuwuAreaAdapter mFuwuAreaAdapter;
    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fu_wu_area);
        ButterKnife.bind(this);
    }*/

    @Override
    public Integer initResid() {
        return R.layout.activity_fu_wu_area;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("服务地区");
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mFuwuareaRc.setLayoutManager(linearLayoutManager);
        mFuwuAreaAdapter = new FuwuAreaAdapter(this, data);
        mFuwuareaRc.setAdapter(mFuwuAreaAdapter);

        final StringBuffer sb = new StringBuffer();
        mFuwuareaOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < data.size(); i++) {
                    Log.e("ischeck", data.get(i).isCheck+"");
                    if (data.get(i).isCheck) {
                        if (i + 1 == data.size()) {
                            sb.append(data.get(i).name);
                        } else {
                            sb.append(data.get(i).name + " ");
                        }
                    }
                }

                Setting.setFuwuArea(sb.toString());
                Setting.setTemp(1);
                finish();

            }
        });


        initData();



    }




    private void initData() {
        MyRequest request = new MyRequest(ConStans.FuwuArea_Url);
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {

                FuWuAreaBean fuWuAreaBean = GsonUtil.GsonToBean(json, FuWuAreaBean.class);

                data.clear();
                data.addAll(fuWuAreaBean.data);
                if (mFuwuAreaAdapter != null) {
                    mFuwuAreaAdapter.notifyDataSetChanged();
                }
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
